package com.waitingsnow.common.share.util
{
import flash.net.URLVariables;
import flash.utils.ByteArray;

public class DataUtils
{
    /**
     *
     * Obtains the query string from the current HTML location
     * and returns its values in a URLVariables instance.
     *
     */
    public static function getURLVariables(url:String):URLVariables
    {
        var params:String;

        if (url.indexOf('#') != -1)
        {
            params = url.slice(url.indexOf('#') + 1);
        }
        else if (url.indexOf('?') != -1)
        {
            params = url.slice(url.indexOf('?') + 1);
        }

        var vars:URLVariables = new URLVariables();

        params && vars.decode(params);
        return vars;
    }
    
    /**
     * Encoding for String
     * Change GB2312 to UTF8
     */
    public static function GB2312toUTF8(input:*):String
    {
        var byte:ByteArray = new ByteArray();
        byte.writeMultiByte(input, "gb2312");
        byte.position = 0;
        return byte.readMultiByte(byte.bytesAvailable, "utf-8");
    }
    
    /**
     *转化成UTF编码  
     * @param str
     * @return 
     * 
     */    
    public static function toUtf( str:String ):String
        
    {
        
        var bytes:ByteArray = new ByteArray ();
        
        bytes.writeUTFBytes( str );
        
        bytes.position = 0;
        
        var rs:String = "";
        
        var code:uint;
        
        while( bytes.bytesAvailable )
            
        {
            
            code = bytes.readUTFBytes(1).charCodeAt(0);
            
            if( code>>4 == 14 ){
                
                bytes.position--;
                
                code = bytes.readUTFBytes(3).charCodeAt(0);
                
            }
                
            else if( code>>5 == 6 ){
                
                bytes.position--;
                
                code = bytes.readUTFBytes(2).charCodeAt(0);
                
            }
            
            rs += code + " ";
            
        }
        
        return rs;
        
    }
    
    /** @private */
    public static function strEscape(value:String):String
    {
        var str:String = encodeURI(value);
        str = str.replace(/\:/g, "%3A");
        str = str.replace(/\'/g, "%27");
        str = str.replace(/\//g, "%2F");
        str = str.replace(/\*/g, "%2A");
        str = str.replace(/\+/g, "%2B");
        str = str.replace(/@/g, "%40");
        return str;
    }
}
}